#include "WSMonitor.hpp"

WSMonitor::WSMonitor(std::mutex& writeMutex,Client* evtHandler,RemoteHandle* remoteHandle) : 
    m_writeMutex(writeMutex),
    _evtHandler(evtHandler),
    _remoteHandle(remoteHandle)
{}

void WSMonitor::onPing(const WebSocket& socket, const oatpp::String& message)
{
    std::lock_guard<std::mutex> lock(m_writeMutex);
    socket.sendPong(message);
}

void WSMonitor::onPong(const WebSocket& socket, const oatpp::String& message)
{
}

void WSMonitor::onClose(const WebSocket& socket, v_uint16 code, const oatpp::String& message)
{
}

void WSMonitor::readMessage(const WebSocket& socket, v_uint8 opcode, p_char8 data, oatpp::v_io_size size)
{
    //OATPP_LOGI("OATPP","Get NewData");
    static long allSize=0;
    if(size == 0) { // message transfer finished
        _remoteHandle->AcceptData((const char*)m_messageBuffer.getData(),allSize);
        m_messageBuffer.setCurrentPosition(0);
        allSize=0;
    } else if(size > 0) { // message frame received
        m_messageBuffer.writeSimple(data, size);
        allSize+=size;
    }
}
